/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillItemListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillItemList.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		List depList = (List) model.get("depList");
		List wcList = (List) model.get("wcList");
		List mtList = (List) model.get("mtList");
		List areaList = (List) model.get("areaList");
		List etList = (List) model.get("etList");
		List elList = (List) model.get("elList");
		List proList = (List) model.get("proList");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		byte colNum = 39;

		int sf;
		for (sf = 1; sf < colNum; ++sf) {
			r.createCell((short) sf);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString("业务明细一览表"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);

		for (sf = 1; sf < colNum; ++sf) {
			r.createCell((short) sf);
		}

		c.setCellValue(new HSSFRichTextString("金额合计（未扣除优惠）："
				+ model.get("costTotal") + "元 面积合计：" + model.get("squareTotal")
				+ "平方米 长度合计：" + model.get("lengMTotal") + "米 "
				+ model.get("lengCmTotal") + "公分 成品数量合计："
				+ model.get("amountTotal") + " 合同单数：" + model.get("billTotal")
				+ "笔"));
		sheet.addMergedRegion(new Region(1, 0, 1, (short) (colNum - 1)));
		r = sheet.createRow(2);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("业务单号"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("订货日期"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("订货单位"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("宽"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("高"));
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(12);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(13);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		c = r.createCell(14);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("生产进度"));
		c = r.createCell(15);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("业务员"));
		c = r.createCell(16);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("设计员"));
		c = r.createCell(17);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("跟单员"));
		c = r.createCell(18);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("业务来源"));
		c = r.createCell(19);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("开单人"));
		c = r.createCell(20);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("部门"));
		c = r.createCell(21);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("工作中心"));
		c = r.createCell(22);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("是否外协"));
		c = r.createCell(23);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("地区"));
		c = r.createCell(24);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("客户类别"));
		c = r.createCell(25);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("客户等级"));
		c = r.createCell(26);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("摘要"));
		c = r.createCell(27);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("联系人"));
		c = r.createCell(28);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("电话"));
		c = r.createCell(29);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("地址"));
		c = r.createCell(30);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("交货日期"));
		c = r.createCell(31);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("结清日期"));
		c = r.createCell(32);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("审核状态"));
		c = r.createCell(33);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("完工状态"));
		c = r.createCell(34);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("送货状态"));
		c = r.createCell(35);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("结清状态"));
		c = r.createCell(36);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("提货要求"));
		c = r.createCell(37);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("合同号"));
		c = r.createCell(38);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("银行账户"));
		SimpleDateFormat arg50 = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			BusinessBill bill = (BusinessBill) objs[0];
			String estName = (String) objs[1];
			BusinessBillItem item = (BusinessBillItem) objs[2];
			String exam = (String) objs[3];
			String clos = (String) objs[4];
			BigDecimal deliAmou = (BigDecimal) objs[5];
			String wcComp = (String) objs[6];
			Date closDate = (Date) objs[7];
			long areaId = ((Long) objs[8]).longValue();
			long etId = ((Long) objs[9]).longValue();
			long elId = ((Long) objs[10]).longValue();
			long proId = ((Long) objs[12]).longValue();
			String baName = (String) objs[13];
			String speStr = item.getSpecification();
			String amountStr = "";
			if (!item.getPriceType().equals("AREA")
					&& !item.getPriceType().equals("LENGTH")) {
				amountStr = EncCommonHelper.parseBigDecimal(item.getAmount());
			} else {
				speStr = speStr + " x "
						+ EncCommonHelper.parseBigDecimal(item.getAmount());
				amountStr = EncCommonHelper
						.parseBigDecimal(item.getSquareNum());
			}

			String unit = "";
			String widt = "";
			String heig = "";
			if (item.getPriceType().equals("AREA")) {
				unit = "平方米";
				widt = EncCommonHelper.parseBigDecimal(item.getLength());
				heig = EncCommonHelper.parseBigDecimal(item.getWidth());
			} else if (item.getPriceType().equals("LENGTH")) {
				if (item.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}

				widt = EncCommonHelper.parseBigDecimal(item.getLength());
				heig = EncCommonHelper.parseBigDecimal(item.getWidth());
			} else {
				unit = item.getUnit();
			}

			String deliDateStr = "";
			String closDateStr = "";
			if (bill.getDeliverDate() != null) {
				deliDateStr = arg50.format(bill.getDeliverDate());
			}

			if (closDate != null) {
				closDateStr = arg50.format(closDate);
			}

			String deliStr = "";
			if (deliAmou.compareTo(item.getAmount()) == 0) {
				deliStr = "Y";
			} else {
				deliStr = "N";
			}

			String isCoop = "否";
			if (item.getIsCoop().equals("Y")) {
				isCoop = "是";
			}

			r = sheet.createRow(os + 3);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getBillCode()));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg50.format(bill
					.getOrderDate())));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(estName));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(mtList, item.getMakeTypeId())));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(item.getFinishedContent()));
			c = r.createCell(5);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(item.getProName()));
			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(widt));
			c = r.createCell(7);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(heig));
			c = r.createCell(8);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(speStr));
			c = r.createCell(9);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(unit));
			c = r.createCell(10);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(amountStr));
			c = r.createCell(11);
			c.setCellStyle(norStyle);
			c.setCellValue(item.getUnitPrice().doubleValue());
			c = r.createCell(12);
			c.setCellStyle(norStyle);
			c.setCellValue(item.getCost().doubleValue());
			c = r.createCell(13);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(item.getMakeRequire()));
			c = r.createCell(14);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(proList, proId)));
			c = r.createCell(15);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getManagerName()));
			c = r.createCell(16);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getDesignerNameStr()));
			c = r.createCell(17);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getFollowerNameStr()));
			c = r.createCell(18);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getIntroducerName()));
			c = r.createCell(19);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getCreaterName()));
			c = r.createCell(20);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(depList, bill.getDepartmentId())));
			c = r.createCell(21);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(wcList, item.getWcId())));
			c = r.createCell(22);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(isCoop));
			c = r.createCell(23);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(areaList, areaId)));
			c = r.createCell(24);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(etList, etId)));
			c = r.createCell(25);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(elList, elId)));
			c = r.createCell(26);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getContent()));
			c = r.createCell(27);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getLinkman()));
			c = r.createCell(28);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getPhone()));
			c = r.createCell(29);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getAddress()));
			c = r.createCell(30);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(deliDateStr));
			c = r.createCell(31);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(closDateStr));
			c = r.createCell(32);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(exam));
			c = r.createCell(33);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(wcComp));
			c = r.createCell(34);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(deliStr));
			c = r.createCell(35);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(clos));
			c = r.createCell(36);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getLadingRequire()));
			c = r.createCell(37);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(bill.getContractCode()));
			c = r.createCell(38);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(baName));
		}

		ServletOutputStream arg51 = response.getOutputStream();
		workbook.write(arg51);
		arg51.flush();
		arg51.close();
	}
}